const dayjs = require('dayjs');
const { getSqlLiteData } = require('../../SqlLiteDB');

module.exports = function setWorkorderRouter (router) {

    //查询订单信息
    router.get('/getWorkorders', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.query['dataContent']);
            let { matcode, wostate, wocusid } = dataContent;
            let SqlString = "SELECT * FROM jm_workorder U WHERE 1=1"
                + (matcode ? ` AND U.matcode LIKE'${matcode}'` : '')
                + (wostate ? ` AND U.wostate LIKE'${wostate}'` : '')
                + (wocusid ? ` AND U.wocusid LIKE'${wocusid}'` : '')
                + " ORDER BY U.wostate,U.wocusid";
            let workorders = await getSqlLiteData(SqlString);

            res.status(200).json({
                workorders,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });

    //修改或添加订单信息
    router.post('/updateWorkorder', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let workorder = dataContent.workorder;
            // let wocusid = dataContent.wocusid;
            // let name = dataContent.name;
            // let remark = dataContent.remark;
            // let user = dataContent.user;

            let SqlString = `REPLACE INTO jm_workorder (jm_workorderid,matcode,woplanqty,woplandate
,wocomdate,wolevel,wocomments,wostate
,wocusid,woactualfinishdate,createtime,creator
,lastprescheduletime)
VALUES ('${workorder.jm_workorderid}','${workorder.matcode}','${workorder.woplanqty}','${workorder.woplandate}'
,'${workorder.wocomdate}','${workorder.wolevel}','${workorder.wocomments}','${workorder.wostate}'
,'${workorder.wocusid}','${workorder.woactualfinishdate}','${workorder.createtime}','${workorder.creator}'
,'${workorder.lastprescheduletime}');`;
            let result = await getSqlLiteData(SqlString);

            res.status(200).json({
                result,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });

    //订单排产
    router.post('/scheduleWorkorder', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let { jm_workorderid, matcode, woplanqty } = dataContent;

            let resultArr = [];
            for (let i = 1; i <= woplanqty; i++) {
                let prosn = jm_workorderid + `${i}`.padStart(8, '0');
                let updateIndexSqlString1 = `INSERT INTO jm_woschedule(prosn,jm_workorderid,matcode) VALUES('${prosn}','${jm_workorderid}','${matcode}')`;
                let resultIndex1 = await getSqlLiteData(updateIndexSqlString1);
                resultArr.push(resultIndex1);
            }

            let nowTimeStr = dayjs().format('YYYY/MM/DD HH:mm:ss');
            let SqlString = `UPDATE jm_workorder SET wostate=1,lastprescheduletime='${nowTimeStr}' WHERE jm_workorderid ='${jm_workorderid}'`;
            let result = await getSqlLiteData(SqlString);

            res.status(200).json({
                result,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });

    //订单完成
    router.post('/finishWorkorder', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let { jm_workorderid, matcode, woplanqty } = dataContent;

            let nowTimeStr = dayjs().format('YYYY/MM/DD HH:mm:ss');
            let SqlString = `UPDATE jm_workorder SET wostate=2,woactualfinishdate='${nowTimeStr}' WHERE jm_workorderid ='${jm_workorderid}'`;
            let result = await getSqlLiteData(SqlString);

            res.status(200).json({
                result,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });


    //删除订单
    router.post('/deleteWorkorder', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let { jm_workorderid } = dataContent;

            let SqlString = `DELETE FROM jm_workorder WHERE jm_workorderid ='${jm_workorderid}'`;
            let result = await getSqlLiteData(SqlString);

            res.status(200).json({
                result,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });
};
